#!/bin/sh
#
# #%L
# Bio-Formats C++ libraries (shell wrapper)
# %%
# Copyright © 2015 Open Microscopy Environment:
#   - Massachusetts Institute of Technology
#   - National Institutes of Health
#   - University of Dundee
#   - Board of Regents of the University of Wisconsin-Madison
#   - Glencoe Software, Inc.
# %%
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of any organization.
# #L%

set -e

# Installation prefix (if any)

INSTALL_PREFIX="@CMAKE_INSTALL_PREFIX@"

# Relative installation paths

INSTALL_BINDIR="@CMAKE_INSTALL_BINDIR@"
INSTALL_SBINDIR="@CMAKE_INSTALL_SBINDIR@"
INSTALL_LIBEXECDIR="@CMAKE_INSTALL_LIBEXECDIR@"
INSTALL_SYSCONFDIR="@CMAKE_INSTALL_SYSCONFDIR@"
INSTALL_SHAREDSTATEDIR="@CMAKE_INSTALL_SHAREDSTATEDIR@"
INSTALL_LOCALSTATEDIR="@CMAKE_INSTALL_LOCALSTATEDIR@"
INSTALL_LIBDIR="@CMAKE_INSTALL_LIBDIR@"
INSTALL_INCLUDEDIR="@CMAKE_INSTALL_INCLUDEDIR@"
INSTALL_OLDINCLUDEDIR="@CMAKE_INSTALL_OLDINCLUDEDIR@"
INSTALL_DATAROOTDIR="@CMAKE_INSTALL_DATAROOTDIR@"
INSTALL_DATADIR="@CMAKE_INSTALL_DATADIR@"
INSTALL_INFODIR="@CMAKE_INSTALL_INFODIR@"
INSTALL_LOCALEDIR="@CMAKE_INSTALL_LOCALEDIR@"
INSTALL_MANDIR="@CMAKE_INSTALL_MANDIR@"
INSTALL_DOCDIR="@CMAKE_INSTALL_DOCDIR@"

# Absolute installation paths

INSTALL_FULL_BINDIR="@CMAKE_INSTALL_FULL_BINDIR@"
INSTALL_FULL_SBINDIR="@CMAKE_INSTALL_FULL_SBINDIR@"
INSTALL_FULL_LIBEXECDIR="@CMAKE_INSTALL_FULL_LIBEXECDIR@"
INSTALL_FULL_SYSCONFDIR="@CMAKE_INSTALL_FULL_SYSCONFDIR@"
INSTALL_FULL_SHAREDSTATEDIR="@CMAKE_INSTALL_FULL_SHAREDSTATEDIR@"
INSTALL_FULL_LOCALSTATEDIR="@CMAKE_INSTALL_FULL_LOCALSTATEDIR@"
INSTALL_FULL_LIBDIR="@CMAKE_INSTALL_FULL_LIBDIR@"
INSTALL_FULL_INCLUDEDIR="@CMAKE_INSTALL_FULL_INCLUDEDIR@"
INSTALL_FULL_OLDINCLUDEDIR="@CMAKE_INSTALL_FULL_OLDINCLUDEDIR@"
INSTALL_FULL_DATAROOTDIR="@CMAKE_INSTALL_FULL_DATAROOTDIR@"
INSTALL_FULL_DATADIR="@CMAKE_INSTALL_FULL_DATADIR@"
INSTALL_FULL_INFODIR="@CMAKE_INSTALL_FULL_INFODIR@"
INSTALL_FULL_LOCALEDIR="@CMAKE_INSTALL_FULL_LOCALEDIR@"
INSTALL_FULL_MANDIR="@CMAKE_INSTALL_FULL_MANDIR@"
INSTALL_FULL_DOCDIR="@CMAKE_INSTALL_FULL_DOCDIR@"

BF_INSTALL_DATADIR="@OME_BIOFORMATS_INSTALL_DATADIR@"
BF_INSTALL_FULL_DATADIR="@OME_BIOFORMATS_INSTALL_FULL_DATADIR@"

BF_INSTALL_ICONDIR="@OME_BIOFORMATS_INSTALL_ICONDIR@"
BF_INSTALL_FULL_ICONDIR="@OME_BIOFORMATS_INSTALL_FULL_ICONDIR@"

BF_INSTALL_LIBEXECDIR="@OME_BIOFORMATS_INSTALL_LIBEXECDIR@"
BF_INSTALL_FULL_LIBEXECDIR="@OME_BIOFORMATS_INSTALL_FULL_LIBEXECDIR@"

BF_INSTALL_SCHEMADIR="@OME_BIOFORMATS_INSTALL_SCHEMADIR@"
BF_INSTALL_FULL_SCHEMADIR="@OME_BIOFORMATS_INSTALL_FULL_SCHEMADIR@"

BF_INSTALL_TRANSFORMDIR="@OME_BIOFORMATS_INSTALL_TRANSFORMDIR@"
BF_INSTALL_FULL_TRANSFORMDIR="@OME_BIOFORMATS_INSTALL_FULL_TRANSFORMDIR@"


# Release version

VERSION_MAJOR="@OME_VERSION_MAJOR@"
VERSION_MINOR="@OME_VERSION_MINOR@"
VERSION_PATCH="@OME_VERSION_PATCH@"
VERSION_EXTRA="@OME_VERSION_EXTRA@"
RELEASE_DATE="@OME_VCS_DATE_S@"

# Program invoked as and command requested (if any)

prog=$0
if [ "$#" -gt 0 ]; then
  cmd=$1
  shift
fi

# Determine installation prefix if unset, then determine paths for
# programs, libraries, manpages etc.

if [ -d "$INSTALL_PREFIX" ]; then
  PATH="$INSTALL_FULL_BINDIR:$PATH"
  LIBEXECDIR="$BF_INSTALL_FULL_LIBEXECDIR"
  MANDIR="$INSTALL_FULL_MANDIR"
  if [ "$(uname -s)" = "Darwin" ]; then
    if [ -z "$DYLD_FALLBACK_LIBRARY_PATH" ]; then
      export DYLD_FALLBACK_LIBRARY_PATH="$INSTALL_FULL_LIBDIR"
    else
      export DYLD_FALLBACK_LIBRARY_PATH="$INSTALL_FULL_LIBDIR:$DYLD_FALLBACK_LIBRARY_PATH"
    fi
  else
    if [ -z "$LD_LIBRARY_PATH" ]; then
      export LD_LIBRARY_PATH="$INSTALL_FULL_LIBDIR"
    else
      export LD_LIBRARY_PATH="$INSTALL_FULL_LIBDIR:$LD_LIBRARY_PATH"
    fi
  fi
else
  bindir=$(dirname "$prog")
  rootdir=$(eval echo "\${bindir%$INSTALL_BINDIR}")
  rootdir=${rootdir%/}
  if [ -z "$rootdir" ]; then
    rootdir=$(pwd)
  fi

  PATH="${rootdir}${INSTALL_FULL_BINDIR}:$PATH"
  LIBEXECDIR="${rootdir}${BF_INSTALL_FULL_LIBEXECDIR}"
  MANDIR="${rootdir}${INSTALL_FULL_MANDIR}"
  if [ "$(uname -s)" = "Darwin" ]; then
    if [ -z "$DYLD_FALLBACK_LIBRARY_PATH" ]; then
      export DYLD_FALLBACK_LIBRARY_PATH="$rootdir$INSTALL_FULL_LIBDIR"
    else
      export DYLD_FALLBACK_LIBRARY_PATH="$rootdir$INSTALL_FULL_LIBDIR:$DYLD_FALLBACK_LIBRARY_PATH"
    fi
  else
    if [ -z "$LD_LIBRARY_PATH" ]; then
      export LD_LIBRARY_PATH="$rootdir$INSTALL_FULL_LIBDIR"
    else
      export LD_LIBRARY_PATH="$rootdir$INSTALL_FULL_LIBDIR:$LD_LIBRARY_PATH"
    fi
  fi
fi

usage() {
  cat <<EOF
usage: $prog [--help] [--usage] <cmd> [<args>]

available bf-test commands in '$LIBEXECDIR'

EOF
  ls -C "$LIBEXECDIR" | sed -e 's;^;	;'
}

version() {
  cat <<EOF
bf-test (OME Bio-Formats) $VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH$VERSION_EXTRA ($RELEASE_DATE)
Copyright © 2006–2015 Open Microscopy Environment

This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
}

help() {
  exec man -M "$MANDIR" 1 bf-test
  exit 1
}

run() {
  runcmd="$1"
  shift

  realcmd="$runcmd"
  if [ "$realcmd" = "info" -o "$realcmd" = "showinf" ]; then
    realcmd=info
  fi
  if [ "$realcmd" = "view" -o "$realcmd" = "glview" ]; then
    realcmd=view
  fi
  realcmd="$LIBEXECDIR/$realcmd"

  if [ ! -x "$realcmd" ]; then
    echo "bf-test: '$runcmd' is not a bf-test command.  See 'bf-test --help'" >&2
    exit 1
  fi

  exec "$realcmd" "$@"
  exit 1
}

# Special case options

if [ -z "$cmd" ]; then
  usage >&2
  exit 1
fi
if [ "$cmd" = "--help" -o "$cmd" = "-h" ]; then
  help
  exit 1
fi

if [ "$cmd" = "--version" -o "$cmd" = "-V" ]; then
  version
  exit 0
fi

if [ "$cmd" = "--usage" -o "$cmd" = "-u" -o -z "$cmd" ]; then
  usage
  exit 0
fi

# Run subcommand
run "$cmd" "$@"
exit 1
